package org.fluidripple.controller;

import io.netty.channel.Channel;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.fluidripple.common.Result;
import org.fluidripple.entity.DeviceEntity;
import org.fluidripple.socket.protocol.*;
import org.fluidripple.socket.session.Session;
import org.fluidripple.socket.session.SessionManager;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/device")
@RequiredArgsConstructor
public class DeviceController {

    private final DeviceStartCollectProtocol deviceStartCollectProtocol;
    private final DeviceStopCollectProtocol deviceStopCollectProtocol;
    private final DeviceSerializableProtocol deviceSerializableProtocol;
    private final DeviceMagnificationProtocol deviceMagnificationProtocol;
    private final SamplingFrequencyProtocol samplingFrequencyProtocol;
    /**
     * 获取在线设备
     */
    @GetMapping("list")
    public Result<List<DeviceEntity>> list(){
        Collection<Session> list = SessionManager.list();
        List<DeviceEntity> deviceEntityList = list.stream().map(v -> {
            DeviceEntity deviceEntity = new DeviceEntity();
            deviceEntity.setDeviceSerializable(v.getDeviceSerializable());
            deviceEntity.setConnectTime(v.getConnectTime());
            deviceEntity.setCollectData(v.isCollectData());
            deviceEntity.setSessionId(v.getSessionId());
            deviceEntity.setMagnification(v.getMagnification());
            deviceEntity.setRemoteAddressStr(v.getRemoteAddressStr());
            deviceEntity.setSamplingFrequency(v.getSamplingFrequency());
            return deviceEntity;
        }).collect(Collectors.toList());
        return Result.sucess(deviceEntityList);
    }

    /**
     * 获取设备编号
     */
    @GetMapping("getDeviceSerializable")
    public Result getDeviceSerializable(@RequestParam("sessionId")String sessionId){
        Session session = SessionManager.getSessionById(sessionId);
        Channel channel = session.getChannel();
        deviceSerializableProtocol.writer(channel);
        return Result.sucess();
    }

    /**
     * 开始采集
     */
    @GetMapping("startCollect")
    public Result startCollect(@RequestParam("deviceSerializable")String deviceSerializable){
        log.info("开启设备采集数据：{}",deviceSerializable);
        Session session = SessionManager.getSession(deviceSerializable);
        Channel channel = session.getChannel();
        deviceStartCollectProtocol.writer(channel);
        return Result.sucess();
    }

    /**
     * 停止采集
     */
    @GetMapping("stopCollect")
    public Result stopCollect(@RequestParam("sessionId")String sessionId){
        log.info("停止设备采集数据：{}",sessionId);
        Session session = SessionManager.getSessionById(sessionId);
        Channel channel = session.getChannel();
        deviceStopCollectProtocol.writer(channel);
        return Result.sucess();
    }

    /**
     * 获取放大倍数
     */
    @GetMapping("getMagnification")
    public Result getMagnification(@RequestParam("sessionId")String sessionId){
        Session session = SessionManager.getSessionById(sessionId);
        deviceMagnificationProtocol.writer(session.getChannel());
        return Result.sucess();
    }

    /**
     * 设置放大倍数
     */
    @GetMapping("setMagnification")
    public Result setMagnification(@RequestParam("sessionId")String sessionId,@RequestParam("a")Integer a,@RequestParam("b")Integer b){
        Session session = SessionManager.getSessionById(sessionId);
        DeviceSetMagnificationProtocol setMagnificationProtocol = new DeviceSetMagnificationProtocol(a,b);
        setMagnificationProtocol.writer(session.getChannel());
        return Result.sucess();
    }

    /**
     * 获取采样频率
     */
    @GetMapping("getSamplingFrequencyApi")
    public Result getSamplingFrequencyApi(@RequestParam("sessionId")String sessionId){
        Session session = SessionManager.getSessionById(sessionId);
        samplingFrequencyProtocol.writer(session.getChannel());
        return Result.sucess();
    }

    /**
     * 设置采样频率
     */
    @GetMapping("setSamplingFrequencyApi")
    public Result setSamplingFrequencyApi(@RequestParam("sessionId")String sessionId,@RequestParam("samplingFrequency")int samplingFrequency){
        SamplingFrequencySetProtocol setSamplingFrequencyProtocol = new SamplingFrequencySetProtocol(samplingFrequency);
        Session session = SessionManager.getSessionById(sessionId);
        setSamplingFrequencyProtocol.writer(session.getChannel());
        return Result.sucess();
    }
}
